from docx import Document
from docx.shared import Pt


def format_word_document(doc_path, output_path):
    doc = Document(doc_path)

    # 设置正文格式
    style = doc.styles['Normal']
    font = style.font
    font.name = '仿宋'
    font.size = Pt(14)  # 4号字对应14磅
    style.paragraph_format.first_line_indent = Pt(14 * 2)  # 首行缩进两字符（1字符约为14磅）
    style.paragraph_format.line_spacing = 1.5
    style.paragraph_format.alignment = 0  # 2两端对齐

    # 设置一级标题格式
    heading1_style = doc.styles['Heading 1']
    heading1_font = heading1_style.font
    heading1_font.name = '黑体'
    heading1_font.size = Pt(14)
    heading1_style.paragraph_format.alignment = 0  # 靠左对齐

    # 设置二级标题格式
    heading2_style = doc.styles['Heading 2']
    heading2_font = heading2_style.font
    heading2_font.name = '楷体'
    heading2_font.size = Pt(14)
    heading2_style.paragraph_format.alignment = 0  # 靠左对齐

    heading3_style = doc.styles['Heading 3']
    heading3_font = heading3_style.font
    heading3_font.name = '仿宋'
    heading3_font.size = Pt(14)
    heading3_style.paragraph_format.alignment = 0  # 靠左对齐

    heading4_style = doc.styles['Heading 4']
    heading4_font = heading4_style.font
    heading4_font.name = '仿宋'
    heading4_font.size = Pt(14)
    heading4_style.paragraph_format.alignment = 0  # 靠左对齐

    heading5_style = doc.styles['Heading 5']
    heading5_font = heading5_style.font
    heading5_font.name = '仿宋'
    heading5_font.size = Pt(14)
    heading5_style.paragraph_format.alignment = 0  # 靠左对齐

    heading6_style = doc.styles['Heading 6']
    heading6_font = heading6_style.font
    heading6_font.name = '仿宋'
    heading6_font.size = Pt(14)
    heading6_style.paragraph_format.alignment = 0  # 靠左对齐

    if "Heading 1" in doc.styles:
        style = doc.styles["Heading 1"]
        paragraph_format = style.paragraph_format
        # 设置编号类型为中文序号
        paragraph_format.numbering = {
            "type": "chinese_counting",
            "start": 1
        }
    if "Heading 2" in doc.styles:
        style = doc.styles["Heading 2"]
        paragraph_format = style.paragraph_format
        # 设置编号类型为带括号的中文序号
        paragraph_format.numbering = {
            "type": "chinese_counting",
            "format": "()",
            "start": 1
        }
    if "Heading 3" in doc.styles:
        style = doc.styles["Heading 3"]
        paragraph_format = style.paragraph_format
        # 设置编号类型为阿拉伯数字序号，无特殊格式，用顿号分隔
        paragraph_format.numbering = {
            "type": "decimal",
            "start": 1
        }
    if "Heading 4" in doc.styles:
        style = doc.styles["Heading 4"]
        paragraph_format = style.paragraph_format
        # 设置编号类型为带括号的阿拉伯数字序号
        paragraph_format.numbering = {
            "type": "decimal",
            "format": "()",
            "start": 1
        }
    if "Heading 5" in doc.styles:
        style = doc.styles["Heading 5"]
        paragraph_format = style.paragraph_format
        # 设置编号类型为阿拉伯数字序号，带右括号格式
        paragraph_format.numbering = {
            "type": "decimal",
            "format": ")",
            "start": 1
        }
    if "Heading 6" in doc.styles:
        style = doc.styles["Heading 6"]
        paragraph_format = style.paragraph_format
        # 设置编号类型为大写字母序号，用顿号分隔
        paragraph_format.numbering = {
            "type": "upperLetter",
            "start": 1
        }
    doc.save(output_path)


if __name__ == "__main__":
    input_doc = 'D:\opt\zpj.docx'
    output_doc = 'D:\opt\zpj_out.docx'
    format_word_document(input_doc, output_doc)
